package com.zzty.train.pjm.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.zzty.train.pjm.dao.TeanExamMapper;
import com.zzty.train.pjm.entity.Group;
import com.zzty.train.pjm.entity.User;
import com.zzty.train.pjm.service.GroupingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.zzty.train.pjm.util.HttpUtil.httpPostWithJSON;

@Service
public class GroupingServiceImpl implements GroupingService {
    @Autowired
    TeanExamMapper teanExamMapper;
    @Value("${getUserByClazzId}")
    String url;
    @Override
    public List<User> getUserByClazzId(User user){
        System.out.println("clazzid"+user.toString());
        String us=null;
        try {
            us = httpPostWithJSON(url,user.toJsonString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<User> users=JSONObject.parseArray(us,User.class);
        System.out.println("userService"+users);
        return users;
    }

    @Override
    public Integer addTeam(Group group){
        System.out.println("group111"+group.toString());
        Integer count=teanExamMapper.addTeam(group);
        System.out.println("group"+count);
        return count;
    }

    @Override
    public List<Group>groupp(Group group){
    List<Group>groups=teanExamMapper.group(group);
    return groups;
    }

    @Override
    public Integer deleteGroup(Group group){
        Integer count=teanExamMapper.deleteGroup(group);
        return count;
    }

    @Value("${getUserById}")
    String url1;
    @Override
    public List<Group>group(Group group){
        List<Group>groups1=new ArrayList<>();
        List<Group>groups=teanExamMapper.group(group);
        System.out.println("groups"+groups.toString());
        for(int i=0;i<groups.size();i++){
            Integer id1=Integer.valueOf(groups.get(i).getUserName1());
            Integer id2=Integer.valueOf(groups.get(i).getUserName2());
            User user=new User();
            User user1=new User();
            user.setId(id1);
            user1.setId(id2);
            String us=null;
            String us1=null;
            try {
                us1 = httpPostWithJSON(url1,user1.toJsonString());
                us = httpPostWithJSON(url1,user.toJsonString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            User user2=JSONObject.parseObject(us1,User.class);
            User user3=JSONObject.parseObject(us,User.class);
            String userName1=user2.getName();
            String userName2=user3.getName();
            Group group1=new Group();
            group1.setId(groups.get(i).getId());
            group1.setUserName1(userName1);
            group1.setUserName2(userName2);
            groups1.add(group1);
        }
        System.out.println("groups1"+groups1.toString());
        return groups1;
    }
}
